package com.derbysoft.dswitch.adapter.ihg.web.action.admin.requestlog;

import com.derbysoft.dswitch.adapter.ihg.common.domain.RequestCode;
import com.derbysoft.dswitch.adapter.ihg.common.domain.ReservationLog;
import com.derbysoft.dswitch.adapter.ihg.common.domain.TransactionDetail;
import com.derbysoft.dswitch.adapter.ihg.common.domain.TransactionLog;
import com.derbysoft.dswitch.adapter.ihg.common.repository.*;
import com.derbysoft.dswitch.adapter.ihg.common.utils.CollectionUtil;
import com.derbysoft.dswitch.adapter.ihg.common.utils.DateTimeUtils;
import com.derbysoft.dswitch.adapter.ihg.web.action.support.PaginateActionSupport;
import com.opensymphony.xwork2.ActionContext;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

public class RequestLogAction extends PaginateActionSupport {

    private static final String DATE_FORMATTER = "yyyyMMdd";

    @Autowired
    private TransactionLogService transactionLogService;

    @Autowired
    private ReservationLogService reservationLogService;

    @Autowired
    private ReservationLogDetailRepository reservationLogDetailRepository;

    @Autowired
    private TransactionLogRepository transactionLogRepository;

    @Autowired
    private TransactionLogDetailService transactionLogDetailService;

    private RequestLogCondition requestCondition;

    private TransactionLog transactionLog;

    private ReservationLog reservationLog;

    private List<TransactionDetail> transactionDetails;

    @Action("list")
    public String list() {
        ActionContext.getContext().setLocale(ServletActionContext.getRequest().getLocale());
        if (requestCondition == null || requestCondition.getCode().equals(RequestCode.OTA_HotelAvailRQ)) {
            String date;
            if (requestCondition != null && StringUtils.isNotBlank(requestCondition.getDate())) {
                date = DateTimeUtils.formatDate(DateTimeUtils.parse(requestCondition.getDate()), DATE_FORMATTER);
            } else {
                date = DateTimeUtils.formatDate(DateTimeUtils.today(), DATE_FORMATTER);
            }

            transactionLogRepository.createTable(date);

            setPaginater(transactionLogService.paginater(requestCondition, getPaginater()));
        } else {
            setPaginater(reservationLogService.paginater(requestCondition, getPaginater()));
        }
        return SUCCESS;
    }

    @Actions({@Action("view-ajax"), @Action("view")})
    public String view() {
        if (requestCondition.getCode() == null) {
            return INPUT;
        }
        if (requestCondition.getCode().equals(RequestCode.OTA_HotelAvailRQ)) {
            Date date = StringUtils.isBlank(requestCondition.getDate())
                    ? DateTimeUtils.today()
                    : DateTimeUtils.parse(requestCondition.getDate().trim());
            transactionLog = transactionLogService.load(requestCondition.getId(), date);
            transactionDetails = transactionLogDetailService.loadAll(requestCondition.getId(), date);
            if (transactionLog != null && CollectionUtil.isNotEmpty(transactionDetails)) {
                transactionLog.setTransactionDetails(transactionDetails);
            }
        } else {
            reservationLog = reservationLogService.load(requestCondition.getId());
            if (reservationLog != null) {
                reservationLog.setReservationDetails(reservationLogDetailRepository.load(reservationLog));
            }
        }
        return SUCCESS;
    }

    public RequestLogCondition getRequestCondition() {
        return requestCondition;
    }

    public void setRequestCondition(RequestLogCondition requestCondition) {
        this.requestCondition = requestCondition;
    }

    public List<TransactionDetail> getTransactionDetails() {
        return transactionDetails;
    }

    public ReservationLog getReservationLog() {
        return reservationLog;
    }

    public TransactionLog getTransactionLog() {
        return transactionLog;
    }
}